package components {
	import common.Icon;
	
	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.controls.Button;
	import mx.events.FlexEvent;
	import flash.events.MouseEvent;

	public class RoomNoBar extends HBox {
		
		//------------------------------------------------------------
        //
        // variables
        //
        //------------------------------------------------------------
        
        private var _requestRooms:ArrayCollection;
        
        [ArrayElementType("mx.controls.Button")]
        private var _rooms:ArrayCollection;
        
        private var defaultBtnOverSkin:Object;
        private var defaultBtnDownSkin:Object;
		
		//------------------------------------------------------------
        //
        // methods
        //
        //------------------------------------------------------------
        
        /**
         * add request room, each company only can request 3 rooms
         */
        public function addRequestRoom(roomID:uint):void {
        	if (_requestRooms.length >= 3 || roomIDExisted(roomID)) {
        		return;
        	}
        	
        	_requestRooms.addItem(roomID);
        	
        	update();
        }
        
        /**
         * return the request rooms list
         */
        public function getReqeustRooms():ArrayCollection {
        	return _requestRooms;
        }
        
        private function roomIDExisted(roomID:uint):Boolean {
        	for each (var id:uint in _requestRooms) {
        		if (roomID == id) {
        			return true;
        		}
        	}
        	return false;
        }
        
        private function update():void {
        	var btn:Button;
        	
        	for (var j:int = 0; j < _rooms.length; ++j) {
        		btn = Button(_rooms[j]);
        		btn.label = "";
        		btn.setStyle("overSkin", defaultBtnOverSkin);
        		btn.setStyle("downSkin", defaultBtnDownSkin);
        	}
        	
        	for (var i:int = 0; i < _requestRooms.length; ++i) {
        		var room:Room = Room(parentApplication.rooms[_requestRooms[i]]);
        		btn = Button(_rooms[i]);
        		btn.label = room.roomNo;
        		btn.setStyle("overSkin", Icon.DEL_BTN_OVER);
        		btn.setStyle("downSkin", Icon.DEL_BTN_OVER);
        	}
        }
        
        //------------------------------------------------------------
        //
        // event handlers
        //
        //------------------------------------------------------------
        
        private function onInit(e:FlexEvent):void {
        	_requestRooms = new ArrayCollection();
        	_rooms = new ArrayCollection();
        	
        	var btn:Button;
        	
        	for (var i:int = 0; i < 3; ++i) {
        		btn = new Button();
        		btn.id = i.toString();
        		btn.width = 70;
        		btn.height = 24;
        		btn.setStyle("styleName", "roomNoButton");
        		btn.addEventListener(MouseEvent.CLICK, onClick);
        		
        		addChild(btn);
        		_rooms.addItem(btn);
        	}
        	
        	defaultBtnOverSkin = btn.getStyle("overSkin");
        	defaultBtnDownSkin = btn.getStyle("downSkin");
        }
        
        private function onClick(e:MouseEvent):void {
        	if (Button(e.target).label != "") {
        		_requestRooms.removeItemAt(int(Button(e.target).id));
        		update();
        	}
        }
        
        //------------------------------------------------------------
        //
        // constructor
        //
        //------------------------------------------------------------
		
		public function RoomNoBar() {
			super();
			
			percentWidth = 100;
			percentHeight = 100;
			
			addEventListener(FlexEvent.INITIALIZE, onInit);
		}
		
	}
}